package com.flot.yazb.widget.GiftView;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.flot.yazb.R;
import com.flot.yazb.base.BApplication;
import com.flot.yazb.base.BDialog;
import com.flot.yazb.base.adapter.recycleview.BRAdapter;
import com.flot.yazb.base.adapter.recycleview.RvHolder;
import com.flot.yazb.model.CommentModel;
import com.flot.yazb.model.other.GifeModel;
import com.flot.yazb.model.user.UserModel;
import com.flot.yazb.home.setting.PayDouBiActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 赠送礼物动画
 * Created by fengdongfei on 2017/9/6.
 */

public class SendGiftDialog extends BDialog {

    private  GiftManage giftManger;
    private  LinearLayout animalLinlayout;
    @BindView(R.id.mrv)
    RecyclerView giftRecycleView;
    @BindView(R.id.credit)
    TextView credit;
    @BindView(R.id.send)
    Button send;
    //礼物金额和图片路径
    public static String[] prices = {"1", "6", "300", "888", "1", "66", "300", "888"};
    public static String[] names = {"随悦", "毛巾", "戒指", "红包", "啤酒", "加油", "大力神", "啦啦队"};
    public static int[] present_1evel = new int[]{R.mipmap.present_1, R.mipmap.present_2, R.mipmap.present_3
            , R.mipmap.present_4, R.mipmap.present_5, R.mipmap.present_6
            , R.mipmap.present_7, R.mipmap.present_8};
    private List<GifeModel> dataList = new ArrayList<>();
    private BRAdapter<GifeModel> gifAdapter;
    private UserModel selfModel=BApplication.getInstance().getUserModel();
    private GifeModel SLModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_gift_view);
        ButterKnife.bind(this);
        getPresentList();
        initAdapter();
        credit.setText(selfModel.credit+"豆币");
        GiftManage.setResetListener(new GiftManage.ResetNum() {
            @Override
            public void reset() {
                clickNum=0;
            }
        });
//        selfModel.credit="50000";
    }

    /**
     * 获取礼物的数据
     */
    private void getPresentList() {
        for (int k = 0; k < present_1evel.length; k++) {
            GifeModel model = new GifeModel();
            if (k == defPosition) {
                model.isSelected = true;
            }
            model.gift_id = k + "";
            model.msg = names[k];
            model.price = prices[k];
            dataList.add(model);
        }

        SLModel=dataList.get(0);
    }

    /**
     * 初始化礼物
     */
    private int defPosition = 0;

    private void initAdapter() {
        giftRecycleView.setLayoutManager(new GridLayoutManager(getContext(), 4));

        gifAdapter = new BRAdapter<GifeModel>(getContext(), R.layout.dialog_gift_item, dataList) {
            @Override
            protected void convert(final RvHolder holder, final GifeModel gifeModel, final int position) {
                if (gifeModel.isSelected) {
                    holder.setBackgroundColor(R.id.root, getContext().getResources().getColor(R.color.orange));
                } else {
                    holder.setBackgroundColor(R.id.root, getContext().getResources().getColor(R.color.white));
                }
                holder.setText(R.id.tv_name, gifeModel.msg);
                holder.setText(R.id.tv_price, gifeModel.price + "豆");
                holder.setBackgroundRes(R.id.ivlogo, present_1evel[position]);
                holder.setOnClickListener(R.id.root, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        holder.setBackgroundColor(R.id.root, getContext().getResources().getColor(R.color.orange));
                        dataList.get(defPosition).isSelected = false;
                        defPosition = position;
                        dataList.get(defPosition).isSelected = true;
                        gifAdapter.notifyDataSetChanged();
                        SLModel=gifeModel;
                    }
                });
            }
        };

        giftRecycleView.setAdapter(gifAdapter);
    }

    public SendGiftDialog(Context context, LinearLayout animal) {
        super(context);
        this.animalLinlayout=animal;
        giftManger = new GiftManage(context, animalLinlayout);
    }

    @Override
    public boolean isCancelable(boolean b) {
        return true;
    }

    @Override
    public int setGravity(int gravity) {
        return Gravity.BOTTOM;
    }

    @Override
    public int setPadWidth(float width) {
        return 0;
    }

    @Override
    public int setPadHeight(float height) {
        return 0;
    }


    private int clickNum=0;
    @OnClick(R.id.send)
    public void onClick() {
        //如果豆币不足,跳转豆币充值页面
        if (!"".equals(selfModel.credit) && null!=selfModel.credit){
            if (Integer.parseInt(selfModel.credit)<Integer.parseInt(prices[defPosition])){
                Intent intent= new Intent(getContext(),PayDouBiActivity.class) ;
                getContext().startActivity(intent);
            }else{
                int money = (Integer.parseInt(selfModel.credit) - Integer.parseInt(prices[defPosition]));
                selfModel.credit=money+"";
                credit.setText(selfModel.credit+"豆币");
                if (money>0){
                    clickNum++;
                    GifeModel vo = new GifeModel();
                    vo.gift_id=SLModel.gift_id+"";
                    vo.msg=SLModel.msg;
                    vo.num=clickNum;
                    vo.thumb=selfModel.avatar;
                    vo.name=selfModel.nicheng;
                    giftManger.addGift(vo);
                    giftManger.showGift();//开始显示礼物
                }
            }
        }
    }

    public void createGift(CommentModel bean){
            clickNum++;
            GifeModel vo = new GifeModel();
            vo.gift_id=SLModel.gift_id+"";
            vo.msg=SLModel.msg;
            vo.num=clickNum;
            vo.thumb=bean.headpic;
            vo.name=bean.username;
            giftManger.addGift(vo);
            giftManger.showGift();//开始显示礼物

    }

}
